<?php

/*
 * All doc on :
 * @link http://beez.exalan.org/wiki/doku.php?id=typerequeteclientserveur
 *
 */

/**
 *
 * A Methode for each actions
 *
 * */
class mainController {

	/**
	 * Welcome Page
	 * @param type $request
	 * @param type $context
	 * @return type 
	 */
	public static function index($request, $context) {
		$context->a_o_products = productTable::a_getAllProduct();
		$context->a_o_axis = axisTable::a_getAllAxis();

		return context::SUCCESS;
	}

	public static function getProductInformationAction($request, $context) {

		$i_ProductId = $request['product_id'];

		//  Object To be returned
		$o_Return = new stdClass();
		$o_Return->o_Product = productTable::o_getProductById($i_ProductId);
		$o_Return->o_Axis = product_axisTable::a_getAxisValuesByProductId($i_ProductId);

		echo json_encode($o_Return);
		return context::NONE;
	}

	public static function getAxedProductInformationAction($request, $context) {

		$a_o_AxedProducts = productTable::a_getAxedProduct();

		$a_ToReturn = array();
		foreach ($a_o_AxedProducts as $o_AxedProduct) {
			$o_Return = new stdClass();
			$o_Return->o_Product = productTable::o_getProductById($o_AxedProduct->id_product);
			$o_Return->o_Axis = product_axisTable::a_getAxisValuesByProductId($o_AxedProduct->id_product);
			
			$a_ToReturn[] = $o_Return;
		}


		echo json_encode($a_ToReturn);
		return context::NONE;
	}
	
	public static function saveProductInformationsIntoDatabase($request, $context) {
		$o_ProductInformationAxis = $request['o_ProductInformationAxis'];
		$i_ProductId = $request['i_ProductId'];
		
		//var_dump($i_ProductId);
		//var_dump($o_ProductInformationAxis);
		
		$isAxed = product_axisTable::isAxed($i_ProductId);
		if($isAxed) {
			echo "TA RACE ".$i_ProductId." ".count($o_ProductInformationAxis);
			product_axisTable::deleteProductInformations($i_ProductId);
		}
			
		for($i=0; $i<count($o_ProductInformationAxis); $i++) {
			$o_new_product_axis = new product_axis();
			$o_new_product_axis->id_product = $i_ProductId;
			$o_new_product_axis->id_axis = $o_ProductInformationAxis[$i][id_axis];
			$o_new_product_axis->value = $o_ProductInformationAxis[$i][value];
			
			//var_dump("-- NOUVEL AXE --");
			//var_dump(" ==> Produit : ".$i_ProductId);
			//var_dump(" ==> Axe : ".$o_ProductInformationAxis[$i][id_axis]. " + Valeur : ".$o_ProductInformationAxis[$i][value]);	

			
			$o_new_product_axis->save();
		}
		
		return context::NONE;
	}

	/**
	 * Test the model 
	 * @author DRAMSY
	 * @param type $request
	 * @param type $context 
	 */
	public static function modelTesting($request, $context) {
		echo "<br>All Axis<br>";
		echo "<pre>";
		print_r(axisTable::a_getAllAxis());
		echo "</pre>";
		echo "========================<br>";

		echo "<br>All Product<br>";
		echo "<pre>";
		print_r(productTable::a_getAllProduct());
		echo "</pre>";
		echo "========================<br>";

		echo "<br>All Axis of product 1<br>";
		echo "<pre>";
		print_r(product_axisTable::a_getAxisValuesByProductId(1));
		echo "</pre>";
		echo "========================<br>";

		die;
	}

}
